/*********** NOTES HEADER *********** 
*This program adds quarters in which the patient is not observed with a claim to the dataset
*  We allow these people to not progress into health outcomes.
*  This should help precision. This is for the full control sample

Datasets used: 
 (1) vertmergers
 (2) PSPats_logitpre
 (3) PS-logit-BENE-FILL-IN-QTR_FINAL2005-2012
 (4) Bene_matchdata
 (5) Bene_matchdata_qtr_2005_2012
  
Datasets created:
 (1) Bene_matchdata_qtr_dups 
 (2) Bene_matchdata_qtr_nodups
 (3) Bene_matchdata_qtr_FILLINPRE
 (4) fullListAnalysis2005-2012


************************************/ 


*  This PROGRAM DIFFERS FROM THE 4b. PROGRAM IN THAT IT CONSIDERS THE FULL SET OF POTENTIAL CONTROLS

set more off
capture log close
clear all
global origData "N:\MedicareClaims-P045601-BE"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients\"
global logs "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2
adopath +  N:\SIL-Common\reghdfe-master\package

*QUARTER LIST 
	tempfile quarterlist
	set obs 32
	gen quarter=.
	local k=0
		forval j=2005(1)2012 {
		 forval i=3(3)12 {
		 local k=`k'+1
		replace quarter=mdy(`i',1,`j') in `k'
		 }
		}
	format quarter %tdD_m_Y 

	save `quarterlist', replace
	clear
*QUARTER LIST 

*LOR
	tempfile lordata
	use BENE_ID xbeta ehat lor visit* lnvisit* using "$dataOut/PSPats_logitpre.dta" 
	save `lordata' 
*LOR

*MERGER VARIABLES
	tempfile mgvars
	use BENE_ID quarter id minqm q_of_merger merger vm* using "$dataProp/PS-logit-BENE-FILL-IN-QTR_FINAL2005-2012.dta"
	rename merger merger_seen
	sort BENE_ID quarter
	save `mgvars', replace
	clear
*MERGER VARIABLES

	use BENE_ID using "$dataOut/Bene_matchdata.dta" 
	duplicates drop

*Cross of people and quarters
cross using `quarterlist'
save `quarterlist', replace


*Getting a quarterly measure of the number of doctors visited
*Main dataset
	use "$dataOut/Bene_matchdata_qtr_2005_2012.dta"  
	 sort BENE_ID 
	 drop PHYS_Z*
	 duplicates tag BENE_ID quarter, gen(tag)
	 keep if tag>0
	save "$dataOut/Bene_matchdata_qtr_dups.dta", replace
	
	collapse (max) PSPEC* q_of_merger DEAD age mx* new* firmct RACE* SEX* merger urban* hosp* hs* pop* , by(BENE* quarter vm* STATE* TAX* docid ssa_sc fips*)
	 duplicates tag BENE_ID quarter, gen(tag)
	 sort BENE_ID quarter BENE_ZIP 
	 by BENE_ID quarter : gen ctr=_n
	 drop if tag>0 & ctr>1
	 drop ctr tag
	save "$dataOut/Bene_matchdata_qtr_dups.dta", replace

	use "$dataOut/Bene_matchdata_qtr_2005_2012.dta"  
	 sort BENE_ID 
	 drop PHYS_Z*
	 duplicates tag BENE_ID quarter, gen(tag)
	 drop if tag>0
	 drop tag
	append using "$dataOut/Bene_matchdata_qtr_dups.dta"

duplicates drop

	 merge m:1 BENE_ID quarter using `quarterlist'
		egen begdate=min(quarter) if _merge==3, by( BENE_ID) 
		sort BENE_ID _merge quarter
		by  BENE_ID: replace begdate = begdate[_N]
	 format begdate %td
 
drop vm*
save "$dataOut/Bene_matchdata_qtr_nodups.dta", replace
	capture drop *merger* q_of_merger 
	summ mx* RACE* SEX*
	sort BENE_ID quarter

	gen ORIGDATAFLAG= RACE1~=.
	*Ys 
		foreach i of varlist mx* P* u* {
			di "`i'"
			by BENE_ID : replace `i' =0 if `i'==. & _n==1
			by BENE_ID : replace `i' =`i'[_n-1] if `i'==. & _n>1
		}
 
 *Xs that don't vary
	sort BENE_ID quarter
		foreach i of varlist  SEX* RACE* BENE_BIRTH BENE_DEATH mxhyp* mxdiab* mxicd* {
			by BENE_ID : replace `i' = `i'[_n-1] if `i'==.  
		}

		foreach i of varlist BENE_ZIP* {
			by BENE_ID : replace `i' = `i'[_n-1] if `i'==""
		}


*Xs that vary
		foreach var of varlist PSPEC*{
			local i = regexr("`var'","PSPEC","psps")
			di "`i'"
			rename `var' `i'
		}
 
		gen dropflag=quarter<begdate
		 replace dropflag=2 if quarter>BENE_DEATH
   
*Family practice
		gen fmp=psps08
		 replace fmp=1 if psps01==1
		 replace fmp=1 if psps11==1
		 replace fmp=1 if psps93==1

		egen pspOther=rowmax(psps41 psps10 psps97 psps02 psps29 psps35 psps13 psps83 psps43 psps04 psps49 psps16 psps01 psps39 psps66 psps26 psps46 psps25 psps47 psps70 psps73 psps65 psps77 psps90 psps14 psps92 psps38 psps44 psps94 psps63 psps03 psps68 psps09 psps33 psps24 psps64 psps81 psps60 psps21 psps78 psps80 psps37 psps40 psps28 psps72 psps36 psps89 psps82 psps98 psps67 psps87 psps76 psps71 psps91 psps12 psps32 psps19 psps85 psps99 psps45 psps84 psps17 psps23 psps75 psps86 psps42 psps74 psps15 psps88 psps62 psps27 psps79 psps95 pspsA5 pspsC0 psps54 pspsA0 pspsC1)

		foreach i of varlist fmp psps30 psps06 psps69 psps18 psps48 psps93 pspOther {
			by BENE_ID : replace `i' =0 if `i'==.
		}

		capture drop age
		gen age=round( (quarter- BENE_BIRTH_DT)/365.25,1)

		capture drop _merge
	save "$dataOut/Bene_matchdata_qtr_FILLINPRE.dta", replace

		capture drop PSTATE* urbani*
		sort BENE_ID
	merge m:1 BENE_ID using "$dataOut/PSPats_logit_final.dta"
		drop _merge lor
	merge m:1 BENE_ID using `lordata'
		drop _merge  

	*For movers, find the relevant ZIP code (keep the moved-to ZIP code)
		capture drop tag
		duplicates tag BENE_ID quarter, gen(tag)

		 replace zip_pat = substr(zip_pat,1,5)
		 replace zip_pat = substr(BENE_ZIP_CD,1,5) if zip_pat==""

	*ZIP code
	sort BENE_ID quarter
	by BENE_ID : gen str9 ZIPlastpd= BENE_ZIP_CD[_n-1] if tag>0
	 by BENE_ID quarter: replace ZIPlastpd= ZIPlastpd[1] if tag>0
	 
	by BENE_ID : gen str5 ziplastpd= substr(BENE_ZIP_CD[_n-1],1,5) if tag>0
	 by BENE_ID quarter: replace ziplastpd= ZIPlastpd[1] if tag>0
	 
	 by BENE_ID : gen str9 ZIPnextpd= BENE_ZIP_CD[_n+1] if tag>0
	 by BENE_ID quarter: replace ZIPnextpd= ZIPnextpd[_N] if tag>0

	by BENE_ID : gen str5 zipnextpd= substr(BENE_ZIP_CD[_n-1],1,5) if tag>0
	 by BENE_ID quarter: replace zipnextpd = zipnextpd[1] if tag>0
	 
	*County  
	by BENE_ID : gen str9 CTYlastpd= BENE_COUNTY_CD[_n-1] if tag>0
	 by BENE_ID quarter: replace CTYlastpd= CTYlastpd[1] if tag>0
	 
	by BENE_ID : gen str9 CTYnextpd= BENE_COUNTY_CD[_n+1] if tag>0
	 by BENE_ID quarter: replace CTYnextpd= CTYnextpd[_N] if tag>0
	 by BENE_ID quarter: replace BENE_COUNTY_CD= CTYnextpd if tag>0
	 
	*State
	by BENE_ID : gen str9 STATEnextpd= STATE_CODE[_n+1] if tag>0
	 by BENE_ID quarter: replace STATEnextpd= STATEnextpd[_N] if tag>0
	 by BENE_ID quarter: replace STATE_CODE= STATEnextpd if tag>0

	by BENE_ID : gen statenextpd= STATE_CODE[_n+1] if tag>0
	 by BENE_ID quarter: replace statenextpd= statenextpd[_N] if tag>0
	 by BENE_ID quarter: replace STATE_CODE= statenextpd if tag>0
 
 
 *base duplicates on whether zip is equal to last period (no keep)
	gen keepvar=1
	 replace keepvar=1-(ZIPlastpd==BENE_ZIP_CD) if  tag>0 
	 replace keepvar=1-(ziplastpd==zip_pat) if  tag>0 
	 replace keepvar=1-(CTYlastpd==BENE_COUNTY_CD) if  tag>0 
	 
	drop tag 

*there are some observations with multiple physician/outcome variables during the period. Take the max
	  keep if keepvar==1
	  capture drop tag
	drop keepvar ZIPlast ziplastpd zipnextpd CTYnext CTYlast BENE_ZIP_CD STATEnextpd statenextpd
	capture drop PSTATE*
	drop if dropflag>0
	 
	capture drop _merge 
	capture drop vm_P* 
	capture drop qtr
	sort BENE_ID quarter
	merge 1:1 BENE_ID quarter using `mgvars'

*Quarter and postmerger
	capture drop qtr
		gen qtr=qofd(quarter)
		format qtr %tq

		egen mnqom = min(q_of_merger) , by(BENE_ID)
		 replace q_of_merger=mnqom if q_of_merger==.
		gen postmerger=q_of_merger<qtr  
		 replace postmerger=0 if postmerger==.
		 replace postmerger=0 if q_of_merger==.
		drop mnqom

		egen vm_provider1= max(vm_PROVIDER1) , by(BENE_ID)
		egen vm_provider2= max(vm_PROVIDER2) , by(BENE_ID)
		egen vm_provider3= max(vm_PROVIDER3) , by(BENE_ID)
		egen newmerger= max(merger_seen) , by(BENE_ID)
	drop merger_seen
		rename newmerger merger_seen

	drop vm_PROVIDER*  
 save "$dataOut/fullListAnalysis2005-2012.dta", replace

 


